const { find: findOrgUnit } = require('./org-units')
const { list: listRooms } = require('./rooms')
const { list: listUsers } = require('./user')
const Mock = require('mockjs')

const ConsumeDetails = []
const roomsPage = listRooms(1, 150)
const movieNames = ['一点半', '山上有鬼']
let id = 1
for (let i = 0; i < roomsPage.data.records.length; ++i) {
  const orgUnit = findOrgUnit(roomsPage.data.records[i].orgUnitId)
  const dmPage = listUsers(1, 3, null, orgUnit.id, 'DM', null, null)
  if (dmPage.data.records.length === 0) {
    continue
  }
  const type = (i % 3) + 1
  let amount = 10000000
  ConsumeDetails.push({
    id: id++,
    type: type,
    orgUnitId: orgUnit.id,
    orgUnitName: orgUnit.name,
    roomId: roomsPage.data.records[i].id,
    roomName: roomsPage.data.records[i].name,
    operator: dmPage.data.records[i % dmPage.data.records.length].phoneNumber,
    movieName: '',
    startTime: Mock.Random.datetime(),
    endTime: Mock.Random.datetime(),
    typeStr: type === 1 ? '微信充值' : (type === 2 ? '支付宝充值' : '管理员直充'),
    amount: amount,
    luckyAmount: 0,
    payAmount: amount,
    accountRemained: amount,
    remark: ''
  })
  for (let j = 1; j <= dmPage.data.records.length; ++j) {
    amount -= (j * 45 - 5)
    ConsumeDetails.push(Mock.mock({
      id: id++,
      type: 0,
      orgUnitId: orgUnit.id,
      orgUnitName: orgUnit.name,
      roomId: roomsPage.data.records[i].id,
      roomName: roomsPage.data.records[i].name,
      operator: dmPage.data.records[j - 1].phoneNumber,
      movieName: movieNames[j % movieNames.length],
      startTime: Mock.Random.datetime(),
      endTime: Mock.Random.datetime(),
      typeStr: '消费支出',
      amount: j * 45,
      luckyAmount: 5,
      payAmount: j * 45 - 5,
      accountRemained: amount,
      remark: ''
    }))
  }
}

function list(orgUnitId, roomId) {
  orgUnitId = orgUnitId ? parseInt(orgUnitId) : null
  roomId = roomId ? parseInt(roomId) : null
  const mocks = ConsumeDetails.filter(item => {
    if (orgUnitId && item.orgUnitId !== orgUnitId) return false
    return !(roomId && item.roomId !== roomId)
  })
  return { success: true, data: mocks }
}

module.exports = { list }
